package org.winexpress

import org.codehaus.groovy.grails.web.json.JSONObject;
import org.codehaus.groovy.grails.web.json.JSONArray;
import grails.converters.JSON

class ProductController {
    
    // REST operations
    // GET
    def show = {
        println 'ProductController >> Show >> START'
        
        ProductManagement pman = new ProductManagement()
        
        if (params.id && Product.exists(params.id)) {    
            def p = pman.getProductByID(params.id)
            render p as JSON
            
            println 'Show product by ID >> ' + p.id + " " + p
        }
        else {
            def allP = pman.listProducts()
            if (allP.length() > 0) {
                render allP as JSON
            }
            else {
//                JSONArray arr = new JSONArray()
                JSONObject o = new JSONObject()
                o.put("404",true)
//                arr.add(o)
                render o as JSON
            }            
            
            println 'Listing all wine products >> ' + allP
        }
        
        println 'ProductController >> Show >> END'
    }
    
    // PUT
    def update = {
        println 'ProductController >> Update >> START'
        
        JSONObject o = request.JSON
        
        ProductManagement pman = new ProductManagement()
        
        def p = pman.getProductByID(o.get("id"))
        if (o.has("name")) { p.name = o.get("name"); println p.name }
        if (o.has("description")) { p.description = o.get("description"); println p.description }
        
        def result = pman.updateProduct(p)
        if (result == true) {
            println 'ProductController >> Update >> ' + result
//            render response as JSON
        }
        else {
            println 'ProductController >> Update >> ' + result
        }
        

        println 'ProductController >> Update >> END'

    }
    
    // DELETE
    def delete = {
        println 'ProductController >> delete >> START'
        
        ProductManagement pman = new ProductManagement()
        
        if (params.id && Product.exists(params.id)) {    
            def result = pman.deleteProduct(id)
            
            if (result == true) {
            println 'ProductController >> delete >> ' + result
    //            render response as JSON
            }
            else {
                println 'ProductController >> delete >> ' + result
            }
        }
        
        println 'ProductController >> delete >> END'
    }
    
    // POST
    def save = {
        println 'ProductController >> Save >> START'
        
        JSONObject o = request.JSON
        
        ProductManagement pman = new ProductManagement()
        
        def result = pman.addProduct(o)
        if (result == true) {
            println 'ProductController >> Save >> ' + result
//            render response as JSON
        }
        else {
            println 'ProductController >> Save >> ' + result
        }
        
        println 'ProductController >> Save >> END'
    }
    
}
